class Node{
    constructor(element) {
        this.element = element;
        this.next = null;
    }   
}

class CircleList{
    constructor(){
        this.head = null; //头元素
        this.count = 0; //记录一共多少人
    }

    getElementAt(index){
        let current = this.head;
        for(let i = 0;i < index;i++){
            current = current.next;
        }
        return current
    }

    //在末尾添加
    push(element){
        let node = new Node(element);
        //1.链表为空
        if(this.head === null){
            this.head = node;
            node.next = this.head;
        }else{
            //2.不为空
            let last = this.getElementAt(this.count-1);
            last.next = node;
            node.next = this.head;
        }
        this.count++;
    }

    /**
     * 
     * @param {number} n 总人数
     * @param {number} k 报数上限
     */
    ysfCircle(n,k){  //9
        for(let i = 1;i<=n;i++){
            this.push(i);
        }
        let helper = this.head; //记录上一个
        let current = this.head;//删除的元素

        while(helper.next != this.head){  //把helper移动到最后一个元素
            helper = helper.next;
        }

        while(this.count > 15){
            for(let i = 0;i<k-1;i++){
                helper = helper.next;
                current = current.next;
            }
            console.log("要淘汰的人",current.element)
            helper.next = current.next;
            current = current.next;
            this.count--;
        }
    }
}

let c = new CircleList();

c.ysfCircle(30,9)

// c.push('a');

// c.push('b');

// console.log(c);